package com.westa.smallshop.shop;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.westa.smallshop.R;
import com.westa.smallshop.common.BaseListAdapter;
import com.westa.smallshop.data.GoodsItem;
import com.westa.smallshop.util.HttpUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by cy on 10/26/16.
 */

class ShopAdapter extends BaseListAdapter<ShopAdapter.GoodsItemHolder, GoodsItem> {

    private ShopFragment mFragment;
    private LayoutInflater mLayoutInflater;


    ShopAdapter(ShopFragment fragment) {
        mFragment = fragment;
        mLayoutInflater = mFragment.getActivity().getLayoutInflater();
    }

    @Override
    public GoodsItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new GoodsItemHolder(mLayoutInflater.inflate(R.layout.item_shop_goods, parent, false));
    }

    @Override
    public void onBindViewHolder(final GoodsItemHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        GoodsItem item = mData.get(position);
        holder.mTvShopGoodsItemName.setText(item.getGoods_name());
        holder.mTvShopGoodsItenPrice.setText(mFragment.getString(R.string.price_format, String.valueOf(item.getGoods_price())));
        holder.mTvShopGoodsItemDes.setText(item.getGoods_desc());
        Glide.with(mFragment)
                .load(HttpUtils.getHost() + item.getGoods_pic())
                .centerCrop()
                .into(holder.mIvShopGoodsItemPic);
        holder.mIvShopGoodsItemSold.setVisibility(item.isState() ? View.INVISIBLE : View.VISIBLE);

    }

    @Override
    public GoodsItem getItemObject(int position) {
        return super.getItemObject(position - 1);
    }

    static class GoodsItemHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_shop_goods_item_pic)
        ImageView mIvShopGoodsItemPic;
        @BindView(R.id.tv_shop_goods_item_name)
        TextView mTvShopGoodsItemName;
        @BindView(R.id.tv_shop_goods_item_des)
        TextView mTvShopGoodsItemDes;
        @BindView(R.id.tv_shop_goods_item_price)
        TextView mTvShopGoodsItenPrice;
        @BindView(R.id.iv_shop_goods_item_sold)
        ImageView mIvShopGoodsItemSold;
//        R.layout.item_shop_goods

        public GoodsItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
